﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.Reflection;

//成长体系grow
namespace asMeishi.web.Models
{
    /// <summary>
    /// 动作(数据库表)
    /// </summary>
    public class actions : MongoDocument
    {
        /// <summary>
        /// 一个动作
        /// </summary>
        public actionEnum action { get; set; }

        /// <summary>
        /// 等级需求
        /// </summary>
        //public int demandForLevel { get; set; }

        /// <summary>
        /// 经验值成长
        /// </summary>
        public int experience { get; set; }

        /// <summary>
        /// hp需求
        /// </summary>
        public int demandForHp { get; set; }

        /// <summary>
        /// 加减hp
        /// </summary>
        public int gainHp { get; set; }

        /// <summary>
        /// 减mp
        /// </summary>
        public int decreaseMp { get; set; }

    }

    /// <summary>
    /// 等级基本类(数据库表)
    /// </summary>
    public class level : MongoDocument
    {
        /// <summary>
        /// 等级名
        /// </summary>
        public string name { get; set; }

        /// <summary>
        /// 代码
        /// </summary>
        public int code { get; set; }

        /// <summary>
        /// 按位存储的权限字段
        /// </summary>
        public int access { get; set; }

        /// <summary>
        /// 达到该等级所需最小分数
        /// </summary>
        public int fraction { get; set; }
    }
    
    /// <summary>
    /// 成长体系静态类
    /// </summary>   
    public static class growExpand
    {
        public static bool isPassAction(this level level, actionEnum aE)
        {
            //按位与
            return (level.access & (int)aE) != 0;
        }

        public static void addAction(this level level, actionEnum aE)
        {
            //按位或
            level.access = level.access | (int)aE;
        }

        public static void removeAction(this level level, actionEnum aE)
        {
            //取反与
            level.access = level.access & ~(int)aE;
        }

        public static List<actionEnum> getActions(this level level)//List<actionEnum>
        {
            List<actionEnum> re = new List<actionEnum>();
            int access = level.access, seed = 1;

            for (int i = 0; i < 16; i++)
            {
                if (access % 2 == 1) re.Add((actionEnum)(seed));
                access = access / 2;
                if (access == 0) break;
                seed = seed * 2;
            }
            return re;
        }

        public static List<Tuple<string, actionEnum, bool>> getAllActions(this level level)
        {
            List<Tuple<string, actionEnum, bool>> re = new List<Tuple<string, actionEnum, bool>>();
            //Tuple<string, int, bool> rere;

            Type type = typeof(actionEnum);
            FieldInfo[] fields = type.GetFields();
            
            foreach (var item in fields)
            {
                if (item.Name == "value__") continue;

                //取得数据
                var actionEnum = (actionEnum)Enum.Parse(type, item.Name);

                //取得title
                object[] objs = item.GetCustomAttributes(typeof(DescriptionAttribute), false);
                DescriptionAttribute da = (DescriptionAttribute)objs[0];
                var title = da.Description;

                //取得是否有权限
                var @checked = isPassAction(level, actionEnum);
                re.Add(new Tuple<string, actionEnum, bool>(title, actionEnum, @checked));
            }

            return re;
        }

    }

    /// <summary>
    /// 可进行动作枚举
    /// </summary>
    public enum actionEnum
    {
        /// <summary>
        /// 修改shop
        /// </summary>
        [Description("修改shop")]
        editShop = 1,
        /// <summary>
        /// 上传图片
        /// </summary>
        [Description("上传图片")]
        upFile = 2,
        [Description("删除shop")]
        deleteShop = 4,
        [Description("创建shop")]
        createShop = 8,
        [Description("写点评")]
        comment = 16,
        [Description("写微评")]
        microComment = 32,
        [Description("写tags")]
        tags = 64,
        [Description("修改店名")]
        renameShop = 128,
        [Description("删除印象")]
        delImpression = 256,
        [Description("删除点评")]
        delComment = 512,
        [Description("删除图片")]
        delShopPic = 1024,
        [Description("修改店基本信息")]
        modifyShopInfo = 2048,
        [Description("管理员")]
        manage = 4096,
        [Description("扩展5")]
        exp5 = 4096,
        [Description("扩展6")]
        exp6 = 8192,
        [Description("扩展7")]
        exp7 = 16384,
        [Description("扩展8")]
        exp8 = 32768
    }

    public enum actionTypeEnum
    {
        str,
        xml,
        json
    }
}
